package cn.vonce.sqlbean.example.controller;

import cn.vonce.sql.bean.Update;
import cn.vonce.sql.helper.Cond;
import cn.vonce.sql.helper.Wrapper;
import cn.vonce.sqlbean.example.common.Result;
import cn.vonce.sqlbean.example.enums.UserStatus;
import cn.vonce.sqlbean.example.model.entity.User;
import cn.vonce.sqlbean.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Update 控制器
 *
 * @author Jovi
 * @version 1.0
 * @email imjovi@qq.com
 * @date 2024/2/27 15:18
 */
@RequestMapping
@RestController
public class UpdateController {

    @Autowired
    private UserService userService;

    /**
     * 根据外部id更新
     *
     * @param id
     * @return
     */
    @PutMapping("updateById")
    public Result updateById(Long id) {
        User user = new User();
        user.setNickName("Vicky");
        user.setAge(19);
        if (userService.updateById(user, id) > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * 根据Bean自身id更新
     *
     * @return
     */
    @PutMapping("updateByBeanId")
    public Result updateByBeanId() {
        User user = new User();
        user.setId(1L);
        user.setNickName("Vicky");
        user.setAge(19);
        if (userService.updateByBeanId(user) > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * 根据Bean自身条件更新
     *
     * @return
     */
    @PutMapping("updateByBean")
    public Result updateByBean() {
        User user = new User();
        user.setId(1L);
        user.setNickName("Vicky");
        user.setAge(19);
        if (userService.updateByBean(user, "id = ${id}") > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * 根据包装器条件更新
     *
     * @return
     */
    @PutMapping("updateBy")
    public Result updateBy() {
        User user = new User();
        user.setNickName("Vicky");
        user.setAge(19);
        if (userService.updateBy(user, Wrapper.where(Cond.eq(User::getStatus, UserStatus.NORMAL)).and(Cond.gt(User::getAge, 18))) > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * 根据条件更新，更新所有字段，包括null的字段
     *
     * @return
     */
    @PutMapping("update")
    public Result update() {
        User user = new User();
        user.setNickName("Vicky");
        user.setAge(19);
        if (userService.update(new Update<User>().bean(user).notNull(false).where().eq(User::getStatus, UserStatus.NORMAL).back()) > 0) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * 根据条件更新
     *
     * @return
     */
    @PutMapping("update2")
    public Result update2() {
        if (userService.update(new Update<User>().set(User::getNickName, "Vicky").set(User::getAge, 19).where().eq(User::getId, UserStatus.NORMAL).back()) > 0) {
            return Result.success();
        }
        return Result.fail();
    }

}
